

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ghttp.h"
#include "ghttp_constants.h"

int http_post(const char *uri, const char *params, int timeout, char *result, int resultLen)
{
    if (uri == NULL || params == NULL)
        return -1;

    ghttp_request *request = NULL;
    ghttp_status status;

    request = ghttp_request_new();

    if (ghttp_set_uri(request, (char *)uri) == -1)
        return -1;
    if (ghttp_set_type(request, ghttp_type_post) == -1)
        return -1;

    ghttp_set_header(request, http_hdr_Content_Type, "application/x-www-form-urlencoded");

    char timeoutStr[10];
    snprintf(timeoutStr, 10, "%d", timeout);

    ghttp_set_header(request, http_hdr_Timeout, timeoutStr);

    ghttp_set_body(request, (char *)params, strlen(params));
    ghttp_prepare(request);
    if ((status = ghttp_process(request)) == ghttp_error)
        return -1;

    char *tmp = ghttp_get_body(request);
    resultLen = ghttp_get_body_len(request);
    memcpy(result, tmp, resultLen);

    ghttp_clean(request);
    ghttp_request_destroy(request);
    return resultLen;
}

int main()
{
    char *buf = (char *)malloc(sizeof(char) * 102400);

    char *postStr = "key=123&value=456";

    http_post("http://192.168.8.100:8900", postStr, 3, buf, 102400);
    return 0;
}